﻿using System.Drawing;

namespace Tetris {
    // ██
    // ██
    class Block1: Block {
        /**
         * 构造方法。
         */
        public Block1() {
            mBlocks[1, 1].mUsed = mBlocks[1, 2].mUsed = mBlocks[2, 1].mUsed = mBlocks[2, 2].mUsed = true;
        }

        /**
         * 进入预览区域。
         * @param drawer    绘图者
         */
        public override void EnterPreview(Graphics drawer) {
            int x = Const.PREVIEW_WIDTH / 2 - Const.GRID_SIZE;
            int y = Const.PREVIEW_HEIGHT / 2 - Const.GRID_SIZE;

            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 2; j++) {
                    Rectangle rect = new Rectangle(x + Const.GRID_SIZE * j, y + Const.GRID_SIZE * i, Const.GRID_SIZE, Const.GRID_SIZE);
                    drawer.FillRectangle(new SolidBrush(mColor), rect);
                    Block.DrawEdge(drawer, rect);
                }
            }
        }

        /**
         * 擦除。
         */
        protected override void Erase() {
            Rectangle rect = new Rectangle(Const.GRID_SIZE * (mLine + 1), Const.GRID_SIZE * (mRow + 1), Const.GRID_SIZE * 2, Const.GRID_SIZE * 2);
            
            base.EraseRect(rect);
        }

        /**
         * 获取底端所在的行数。底端是指方块最下方的有效格。
         * @return 底端所在的行数
         */
        public override int GetBottom() {
            return mRow + 2;
        }

        /**
         * 获取顶端所在的行数。顶端是指方块最上方的有效格。
         * @return 顶端所在的行数
         */
        public override int GetTop() {
            return mRow + 1;
        }

        /**
         * 对不能下落的处理。
         */
        protected override void HandleCannotMoveDown() {
            Game.sExistentBlocks[mRow + 1, mLine + 1].mUsed = 
            Game.sExistentBlocks[mRow + 1, mLine + 2].mUsed = 
            Game.sExistentBlocks[mRow + 2, mLine + 1].mUsed = 
            Game.sExistentBlocks[mRow + 2, mLine + 2].mUsed = true;
            Game.sExistentBlocks[mRow + 1, mLine + 1].mColor =
            Game.sExistentBlocks[mRow + 1, mLine + 2].mColor =
            Game.sExistentBlocks[mRow + 2, mLine + 1].mColor =
            Game.sExistentBlocks[mRow + 2, mLine + 2].mColor = mColor;
        }

        /**
         * 下落。
         * @return 成功则返回 true，否则返回 false
         */
        public override bool MoveDown() {
            if (mRow == 17) {
                this.HandleCannotMoveDown();
                return false;
            }
            if (Game.sExistentBlocks[mRow + 3, mLine + 1].mUsed || 
                Game.sExistentBlocks[mRow + 3, mLine + 2].mUsed) {
                this.HandleCannotMoveDown();
                return false;
            }
    
            return base.MoveDown();
        }

        /**
         * 左移。
         */
        public override void MoveLeft() {
            if (mLine == -1) {
                return;
            }
            if (Game.sExistentBlocks[mRow + 1, mLine].mUsed || 
                Game.sExistentBlocks[mRow + 2, mLine].mUsed) {
                return;
            }

            base.MoveLeft();
        }

        /**
         * 右移。
         */
        public override void MoveRight() {
            if (mLine == 7) {
                return;
            }
            if (Game.sExistentBlocks[mRow + 1, mLine + 3].mUsed || 
                Game.sExistentBlocks[mRow + 2, mLine + 3].mUsed) {
                return;
            }

            base.MoveRight();
        }

        /**
         * 变形。
         */
        public override void Transform() {
        }
    }
}
